using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WindowsApplication4
{
    public class Village
    {
        private VillageIntel intel;
        private DataRow drVillage;

        public int X
        {
            get
            {
                return Convert.ToInt32(drVillage[World.CONSTS.VillagesTable.Columns.XIndex]);
            }
        }
        public int y
        {
            get
            {
                return Convert.ToInt32(drVillage[World.CONSTS.VillagesTable.Columns.YIndex]);
            }
        }
        public string name
        {
            get
            {
                return drVillage[World.CONSTS.VillagesTable.Columns.NameIndex].ToString();
            }
        }


        public override string ToString()
        {
            return name + " - " + X.ToString() + "|" + y.ToString();
        }

        public Village(DataRow village)
        {
            drVillage = village;


        }

        public int Points
        {
            get
            {
                if (drVillage != null)
                {
                    return Convert.ToInt32(drVillage[World.CONSTS.VillagesTable.Columns.PointsIndex]);
                }
                else { return 0; }
            }
        }

        public int ID
        {
            get
            {
                if (drVillage != null)
                {
                    return Convert.ToInt32(drVillage[World.CONSTS.VillagesTable.Columns.IdIndex]);
                }
                else { return 0; }
            }
        }

        public Player VillageOwner
        {
            get
            {
                if (drVillage != null)
                {
                    DataRow drPlayer = drVillage.GetParentRow(Worlds.currentWorld.dtVillages.ParentRelations[0]);
                    if (drPlayer == null)
                    {
                        return null;
                    }
                    else
                    {
                        Player player = new Player(drPlayer);

                        if (player.ID == 0) // abondoned village
                        {
                            return null;
                        }
                        else
                        {
                            return player;
                        }
                    }

                }
                else { return null; }

            }

        }

        /// <summary>
        /// may return null of no intelligence is available
        /// </summary>
        public VillageIntel VillageIntelligence
        {
            get
            {
                if (intel == null)
                {
                    DataRow[] drIntels = drVillage.GetChildRows(World.CONSTS.Relations.VillagesToReports);
                    if (drIntels.Length != 0)
                    {
                        intel = new VillageIntel(drIntels[0]);
                    }
                }
                return intel;
            }
        }

        public VillageIntel CheckForLatestIntel()
        {
            VillageIntel intel = this.VillageIntelligence;
            DateTime lastCheckedOn = DateTime.MinValue;
            if (intel != null ) 
            {
                lastCheckedOn = intel.ReportOn;
            }

            ReportRetrieve.ReportRetrieve retrieve = new WindowsApplication4.ReportRetrieve.ReportRetrieve();
            DataSet ds = retrieve.Retrieve(this.X, this.y, new Guid("1B0FBEF2-1DF4-4183-BE57-F58BCA7BF154"), lastCheckedOn);


            if (ds.Tables[0].Rows.Count != 0)
            {
                //if (intel != null)
                //{
                //    intel.Delete();
                //    this.intel = null;
                //}
                //Worlds.currentWorld.AddReport(this, ds.Tables[0].Rows[0]);

                SaveLatestIntel(ds.Tables[0].Rows[0]);
            }

            return VillageIntelligence;
        }

        internal void SaveLatestIntel(DataRow drIntel)
        {
            if (this.VillageIntelligence != null)
            {
                this.VillageIntelligence.Delete();
                this.intel = null;
            }
            Worlds.currentWorld.AddReport(this, drIntel);
        }

        public Distance DistanceTo(Village desVillage)
        {
            return DistanceTo(desVillage.X, desVillage.y);
        }

        public Distance DistanceTo(int desX, int desY)
        {
            return new Distance(X, y, desX, desY);
        }

        
        public override bool  Equals(object obj)
        {
            if (obj is Village)
            {
                Village vil = (Village)obj;
                if (this.X == vil.X && this.y == vil.y)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return base.Equals(obj);
            }

        }
    }
}
